package com.weiwu.weiwustore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiwu.weiwustore.entity.Apply_record;
import com.weiwu.weiwustore.entity.Record;
import com.weiwu.weiwustore.entity.User;
import com.weiwu.weiwustore.mapper.RecordMapper;
import com.weiwu.weiwustore.mapper.UserMapper;
import com.weiwu.weiwustore.pojo.RecordVo;
import com.weiwu.weiwustore.service.RecordService;
import com.weiwu.weiwustore.service.UserService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.weiwu.weiwustore.utils.ObjectFormatUtil.toInteger;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {
    @Autowired
    RecordMapper recordMapper;

    @Override
    public List<RecordVo> getmaterials(Map<String, String> map) {
        // 材料名称 仓库id 开始时间 截止时间
        String mname = "", storeid = "";
        Integer begin = 0, size = 10;
        if (map.containsKey("size")) {
            size = toInteger(map.get("size"));
        }
        if (map.containsKey("page")) {
            Integer page = ObjectFormatUtil.toInteger(map.get("page"));
            begin = (page * size) - size;
        }
        if (map.containsKey("mname")) {
            if (!Objects.equals(map.get("mname"), "")) {
                mname = map.get("mname");
            }
        }
        // 先验证权限 后跟进仓库id
        if (map.containsKey("role") && map.containsKey("storeid")) {
            if (Objects.equals(map.get("role"), "系统管理员") || Objects.equals(map.get("role"), "仓库主管")) {
                storeid = "";
            } else {
                if (!Objects.equals(map.get("storeid"), "")) {
                    storeid = map.get("storeid");
                }
            }
        }
        // 时间处理
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        if (map.containsKey("startDate") && map.containsKey("endDate")) {
            try {
                startDate = format.parse(String.valueOf(map.get("startDate")));
                endDate = format.parse(String.valueOf(map.get("endDate")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 出库的
        List<RecordVo> outsource = recordMapper.getmaterials("出库", storeid, mname, startDate, endDate, begin, size);
        // 入库的
        List<RecordVo> insource = recordMapper.getmaterials("入库", storeid, mname, startDate, endDate, begin, size);
        List<RecordVo> combinedList = new ArrayList<>(outsource);
        if (combinedList.addAll(insource)) {
            return combinedList;
        }
        return new ArrayList<>();
    }
}
